package com.powernode.utils;

import com.powernode.common.Constants;
import com.powernode.domain.SysUser;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 描述：解耦的方式通过静态方法获取request  session applicaiton
 * 作者：动力节点·武汉
 */
public class WebUtils {

    /**
     * 得到request
     * ThreadLocal
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getRequest();
    }


    /**
     * 得到request
     * ThreadLocal
     */
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    /**
     * 得到当前登陆的用户对象
     */
    public static SysUser getCurrentUser(){
        return (SysUser) getSession().getAttribute(Constants.SESSION_USER_KEY);
    }
}
